//
//  WKMessageViewController.m
//  WeKeTang
//
//  Created by ichina on 2017/9/8.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKMessageViewController.h"
#import "WKNetworkTools.h"
#import "WKLoginHandel.h"
#import <WebKit/WebKit.h>

@interface WKMessageViewController ()<WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;

@property (nonatomic, strong) UIProgressView *progressView;//进度条

@end

@implementation WKMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}
- (void)setupUI{
    
    self.navgationBar.barTintColor = RGBColor(40, 188, 203);
    self.statusbar.backgroundColor = RGBColor(40, 188, 203);
    
    [self setLeftButtonWithImage:@"icon_return_white" highlightedImage:@"icon_return_white" target:self action:@selector(backAction)];
    self.navItem.title = @"消息";
    [self middleTitleColor:[UIColor whiteColor]];
    self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 65, kDeviceScreenWidth, kDeviceScreenHeight-64)];
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
    
    self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 64, kDeviceScreenWidth, 1)];
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 0.6f);
    self.progressView.backgroundColor = RGBColor(84, 178, 240);
    self.progressView.progressTintColor = [UIColor yellowColor];
    self.progressView.trackTintColor = [UIColor whiteColor];
    [self.view addSubview:self.progressView];
    NSString *userid = [WKLoginHandel getLoginDataWithString:@"userid"];
   // http://www.dezhi.com/andriodwk/notice
    NSURL *url = [NSURL URLWithString:[NSString  stringWithFormat:@"%@/andriodwk/notice/userid/%@",kTestMainUrl,userid]];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];

}
- (void)backAction{
    
    if (self.backBlock) {
        self.backBlock();
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"message" object:nil];
    [super backAction];
    
}
- (void)loadMessageCount{
    
    __weak typeof(self) weakSelf = self;
    NSString *userid = [WKLoginHandel getLoginDataWithString:@"userid"];
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setObject:@"noticenews" forKey:@"commandid"];
    [para setObject:userid forKey:@"userid"];
    
    NSLog(@"消息参数:%@",para);
    
    [[WKNetworkTools shareManger]setHttpMethod:@"POST"];
    [[WKNetworkTools shareManger]RunNoneRequest:para path:kServerDomain tip:nil completionHandler:^(id responseObject) {
        
    } errorHandler:^(NSError *error) {
        
        [weakSelf showCustomAlertMessage:@"网络错误"];
    } finishHandler:^(id responseObject) {
        
        NSDictionary *dict = [responseObject mj_JSONObject];
        NSDictionary *head = dict[@"head"];
        NSString *body = dict[@"body"];
        NSString *statusCode = head[@"statuscode"];
        if ([statusCode intValue] == 100) {
            
        }
        if ([body intValue] == 1) {
           
            
        }else if ([body intValue] == 0){
            
            
        }
        
    }];
    
}

//拦截标签点击时主动发送请求
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    
    decisionHandler(WKNavigationActionPolicyAllow);
}
//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    //开始加载网页时展示出progressView
    self.progressView.hidden = NO;
    //虚拟进度
    [self.progressView setProgress:0.2 animated:YES];
    //开始加载网页的时候将progressView的Height恢复为1倍
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
    //防止progressView被网页挡住
    [self.view bringSubviewToFront:self.progressView];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation{
    //虚拟进度
    [self.progressView setProgress:0.6 animated:YES];
    
}
//加载完成
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [self.progressView setProgress:1 animated:YES];
    //加载完成后隐藏progressView
    self.progressView.hidden = YES;
    [self loadMessageCount];
    
//    self.navItem.title = self.webView.title;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

@end
